*Write a permanent log file;
PROC PRINTTO LOG="C:\Research\Alcohol and Morbidity\7. Code Archive\Data Files and Code That Produced Them\P09 Inpatient Compute rates.log" NEW;
RUN;

*Write a permanent list file;
PROC PRINTTO PRINT="C:\Research\Alcohol and Morbidity\7. Code Archive\Data Files and Code That Produced Them\P09 Inpatient Compute rates.lst" NEW;
RUN;

/*
Compute the rate of admission 
*/

libname green "G:\temp";

options obs = max compress = yes;

*Combine the inpatient records;
data inter.all_inp;
   format TOTCHG 12.;
   set CA_IA_WI_inp (drop = asource AMONTH AYEAR BMONTH BYEAR) 
       Arizona_inp (drop = asource file_year AMONTH AYEAR BMONTH BYEAR) 
       New_york_inp (drop =  file_year AMONTH AYEAR BMONTH BYEAR) 
       green.Texas_inp (drop =  file_year AMONTH AYEAR BMONTH BYEAR) ;
run;

*Make sure variables are ok;
proc freq data = inter.all_inp;
   tables female state preg_related;
run;

*Make sure variables are ok - female missing 5 percent of time in TX and CA;
proc freq data = inter.all_inp;
   tables state*miss_female;
run;


*Create age profiles by group;
%macro b_group(o_file,suf,a_where);
proc means data = inter.all_inp noprint;
   var  visit alcohol_any injury inj_by_oth inj_by_self inj_accident illness injury_or_alc;
   output out = &o_file. sum = visit_&suf. alcohol_any_&suf. injury_&suf. inj_by_oth_&suf. inj_by_self_&suf. inj_accident_&suf. illness_&suf. injury_or_alc_&suf.;
   class months_21;
   where state in ('AZ','NY','TX','WI') &a_where.;
run;
%mend;
%b_group(all,all,);*Create overall age profile in counts;
%b_group(all_np,all_np,and preg_related = 0);*Create age profile for all non pregnant;
%b_group(female,f,and female = 1);*Create age profile for women;
%b_group(female_np,f_np,and female = 1 and preg_related = 0);*Create age profile for women not in for pregnancy;
%b_group(male,m,and female =  0);*Create age profile for men;

*By state;
%macro by_st(ST);
proc means data = inter.all_inp noprint;
   var  visit alcohol_any injury inj_by_oth inj_by_self inj_accident illness injury_or_alc;
   output out = all_&ST. sum = visit_&ST._NP alcohol_any_&ST._NP injury_&ST._NP inj_by_oth_&ST._NP inj_by_self_&ST._NP inj_accident_&ST._NP illness_&ST._NP injury_or_alc_&ST._NP;
   class months_21;
   where state = "&ST."  and preg_related = 0;
run;
%mend;
%by_st(AZ);
%by_st(CA);
%by_st(IA);
%by_st(NY);
%by_st(TX);
%by_st(WI);

data counts_inp (drop = _type_ _freq_);
   merge all all_np male female female_np all_AZ all_CA all_IA all_Ny all_TX all_WI;
   by months_21;
  *Drop the total count generated by the proc means;
   if months_21 = . then delete;
   merge_v = 1;
run;

data inter.alc_rates_inp (keep = months_21 age bmonth post linear linear_post age_months age_c age_c_sq over age_c_post age_c_post_sq dummy21 
		visit_all_r 	alcohol_any_all_r 		injury_all_r 	inj_by_self_all_r 		inj_by_oth_all_r 	inj_accident_all_r 	 	illness_all_r 		injury_or_alc_all_r
		visit_all_np_r 	alcohol_any_all_np_r 	injury_all_np_r inj_by_self_all_np_r 	inj_by_oth_all_np_r inj_accident_all_np_r  	illness_all_np_r 	injury_or_alc_all_np_r
		visit_m_r 		alcohol_any_m_r 		injury_m_r 		inj_by_self_m_r 		inj_by_oth_m_r 		inj_accident_m_r 	 	illness_m_r			injury_or_alc_m_r
		visit_f_r 		alcohol_any_f_r 		injury_f_r 		inj_by_self_f_r 		inj_by_oth_f_r 		inj_accident_f_r 	 	illness_f_r			injury_or_alc_f_r
		visit_f_np_r	alcohol_any_f_np_r 		injury_f_np_r 	inj_by_self_f_np_r 		inj_by_oth_f_np_r 	inj_accident_f_np_r  	illness_f_np_r		injury_or_alc_f_np_r
		visit_AZ_NP_r	alcohol_any_AZ_NP_r		injury_AZ_NP_r	iNJ_by_self_AZ_NP_r		iNJ_by_oth_AZ_NP_r	iNJ_accident_AZ_NP_r	illness_AZ_NP_r		injury_or_alc_AZ_NP_r
		visit_CA_NP_r	alcohol_any_CA_NP_r		injury_CA_NP_r	iNJ_by_self_CA_NP_r		iNJ_by_oth_CA_NP_r	iNJ_accident_CA_NP_r	illness_CA_NP_r		injury_or_alc_CA_NP_r
		visit_IA_NP_r	alcohol_any_IA_NP_r		injury_IA_NP_r	iNJ_by_self_IA_NP_r		iNJ_by_oth_IA_NP_r	iNJ_accident_IA_NP_r	illness_IA_NP_r		injury_or_alc_IA_NP_r
		visit_NY_NP_r	alcohol_any_NY_NP_r		injury_NY_NP_r	iNJ_by_self_NY_NP_r		iNJ_by_oth_NY_NP_r	iNJ_accident_NY_NP_r	illness_NY_NP_r		injury_or_alc_NY_NP_r
		visit_TX_NP_r	alcohol_any_TX_NP_r		injury_TX_NP_r	iNJ_by_self_TX_NP_r		iNJ_by_oth_TX_NP_r	iNJ_accident_TX_NP_r	illness_TX_NP_r		injury_or_alc_TX_NP_r
		visit_WI_NP_r	alcohol_any_WI_NP_r		injury_WI_NP_r	iNJ_by_self_WI_NP_r		iNJ_by_oth_WI_NP_r	iNJ_accident_WI_NP_r	illness_WI_NP_r    	injury_or_alc_WI_NP_r);
   merge counts_inp inter.alc_pop_combined_inp;
   by merge_v;
  *Compute the age variable;
   age = 21+months_21/12;
  *Compute the running variable and month dummy;
   if months_21 = 0 then bmonth = 1; else bmonth = 0;
   if months_21 >=0 then post = 1; else post = 0;
   linear = months_21;
   linear_post = months_21*post;
  *Arrays for all;
   array all{8} 		visit_all 		alcohol_any_all 		injury_all 			inj_by_self_all 		inj_by_oth_all 		inj_accident_all  		illness_all			injury_or_alc_all;
   array all_r{8} 		visit_all_r 	alcohol_any_all_r 		injury_all_r 		inj_by_self_all_r 		inj_by_oth_all_r 	inj_accident_all_r  	illness_all_r		injury_or_alc_all_r;
  *Arrays for all not pregnant;
   array all_np{8} 		visit_all_np 	alcohol_any_all_np 		injury_all_np 		inj_by_self_all_np 		inj_by_oth_all_np 	inj_accident_all_np  	illness_all_np		injury_or_alc_all_np;
   array all_np_r{8} 	visit_all_np_r 	alcohol_any_all_np_r 	injury_all_np_r 	inj_by_self_all_np_r 	inj_by_oth_all_np_r inj_accident_all_np_r  	illness_all_np_r	injury_or_alc_all_np_r;
  *Arrays by gender;
   array male{8} 		visit_m 		alcohol_any_m 		injury_m 		inj_by_self_m 		inj_by_oth_m 		inj_accident_m 	 	illness_m		injury_or_alc_m;
   array male_r{8} 		visit_m_r 		alcohol_any_m_r 	injury_m_r 		inj_by_self_m_r 	inj_by_oth_m_r 		inj_accident_m_r  	illness_m_r		injury_or_alc_m_r;
   array female{8} 		visit_f 		alcohol_any_f 		injury_f 		inj_by_self_f 		inj_by_oth_f 		inj_accident_f  	illness_f		injury_or_alc_f;
   array female_r{8} 	visit_f_r 		alcohol_any_f_r 	injury_f_r 		inj_by_self_f_r 	inj_by_oth_f_r 		inj_accident_f_r  	illness_f_r		injury_or_alc_f_r;
   array female_np{8} 	visit_f_np 		alcohol_any_f_np 	injury_f_np	 	inj_by_self_f_np 	inj_by_oth_f_np 	inj_accident_f_np  	illness_f_np	injury_or_alc_f_np;
   array female_np_r{8} visit_f_np_r 	alcohol_any_f_np_r 	injury_f_np_r 	inj_by_self_f_np_r 	inj_by_oth_f_np_r 	inj_accident_f_np_r	illness_f_np_r	injury_or_alc_f_np_r;
  *Arrays by state;
   array AZ{8} visit_AZ_NP alcohol_any_AZ_NP injury_AZ_NP inj_by_self_AZ_NP inj_by_oth_AZ_NP inj_accident_AZ_NP illness_AZ_NP	injury_or_alc_AZ_NP;
   array CA{8} visit_CA_NP alcohol_any_CA_NP injury_CA_NP inj_by_self_CA_NP inj_by_oth_CA_NP inj_accident_CA_NP illness_CA_NP	injury_or_alc_CA_NP;
   array IA{8} visit_IA_NP alcohol_any_IA_NP injury_IA_NP inj_by_self_IA_NP inj_by_oth_IA_NP inj_accident_IA_NP illness_IA_NP	injury_or_alc_IA_NP;
   array NY{8} visit_NY_NP alcohol_any_NY_NP injury_NY_NP inj_by_self_NY_NP inj_by_oth_NY_NP inj_accident_NY_NP illness_NY_NP	injury_or_alc_NY_NP;
   array TX{8} visit_TX_NP alcohol_any_TX_NP injury_TX_NP inj_by_self_TX_NP inj_by_oth_TX_NP inj_accident_TX_NP illness_TX_NP	injury_or_alc_TX_NP;
   array WI{8} visit_WI_NP alcohol_any_WI_NP injury_WI_NP inj_by_self_WI_NP inj_by_oth_WI_NP inj_accident_WI_NP illness_WI_NP	injury_or_alc_WI_NP;
   array AZ_r{8} visit_AZ_NP_r alcohol_any_AZ_NP_r injury_AZ_NP_r iNJ_by_self_AZ_NP_r iNJ_by_oth_AZ_NP_r iNJ_accident_AZ_NP_r illness_AZ_NP_r	injury_or_alc_AZ_NP_r;
   array CA_r{8} visit_CA_NP_r alcohol_any_CA_NP_r injury_CA_NP_r iNJ_by_self_CA_NP_r iNJ_by_oth_CA_NP_r iNJ_accident_CA_NP_r illness_CA_NP_r	injury_or_alc_CA_NP_r;
   array IA_r{8} visit_IA_NP_r alcohol_any_IA_NP_r injury_IA_NP_r iNJ_by_self_IA_NP_r iNJ_by_oth_IA_NP_r iNJ_accident_IA_NP_r illness_IA_NP_r	injury_or_alc_IA_NP_r;
   array NY_r{8} visit_NY_NP_r alcohol_any_NY_NP_r injury_NY_NP_r iNJ_by_self_NY_NP_r iNJ_by_oth_NY_NP_r iNJ_accident_NY_NP_r illness_NY_NP_r	injury_or_alc_NY_NP_r;
   array TX_r{8} visit_TX_NP_r alcohol_any_TX_NP_r injury_TX_NP_r iNJ_by_self_TX_NP_r iNJ_by_oth_TX_NP_r iNJ_accident_TX_NP_r illness_TX_NP_r	injury_or_alc_TX_NP_r;
   array WI_r{8} visit_WI_NP_r alcohol_any_WI_NP_r injury_WI_NP_r iNJ_by_self_WI_NP_r iNJ_by_oth_WI_NP_r iNJ_accident_WI_NP_r illness_WI_NP_r	injury_or_alc_WI_NP_r;
  *Compute rates;
   do i = 1 to 8;
      all_r{i} = 10000*all{i}/pop_all_AZ_NY_TX_WI;
	  all_np_r{i} = 10000*all_np{i}/pop_all_AZ_NY_TX_WI;
      male_r{i} = 10000*male{i}/pop_male_AZ_NY_TX_WI;
      female_r{i} = 10000*female{i}/pop_female_AZ_NY_TX_WI;
      female_np_r{i} = 10000*female_np{i}/pop_female_AZ_NY_TX_WI;
      AZ_r{i} = 10000*AZ{i}/pop_AZ;
      CA_r{i} = 10000*CA{i}/pop_CA;
      IA_r{i} = 10000*IA{i}/pop_IA;
      NY_r{i} = 10000*NY{i}/pop_NY;
      TX_r{i} = 10000*TX{i}/pop_TX;
      WI_r{i} = 10000*WI{i}/pop_WI;
   end;
   age_months = months_21/12 +21;    
   age_c = age_months - 21;  
   age_c_sq = age_c*age_c;
   if age_c >= 0 then over = 1; 
   if age_c < 0 then over = 0; 
   age_c_post = over*age_c ;
   age_c_post_sq = age_c_post*age_c_post;  
   if age_months = 21 then dummy21 = 1; else dummy21 = 0; 
run;

*Output this to a CSV file;
PROC EXPORT DATA= inter.alc_rates_inp
            OUTFILE= "C:\Research\Alcohol and Morbidity\7. Code Archive\Data Files and Code That Produced Them\P09 Inpatient Analysis File.dta"
            REPLACE;
RUN;

*Clean up;
proc datasets;
   delete counts all male female all_AZ all_CA all_IA all_NJ all_WI;
run;

*Direct list file back to lst window;
PROC PRINTTO PRINT=PRINT;
RUN; 

*Direct log file back to log window;
PROC PRINTTO LOG=LOG;
RUN; 
